#!/usr/bin/env python3
"""
简单的测试运行器，用于验证聊天功能
"""

import asyncio
import sys
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))


async def run_all_tests():
    """运行所有测试"""
    print("🚀 开始运行测试...")
    
    try:
        print("\n📋 测试1: 聊天功能与考试上下文集成")
        from test_chat_integration import test_chat_with_exam_context
        await test_chat_with_exam_context()
        print("✅ 测试1通过")
        
        print("\n📋 测试2: 模板渲染功能")
        test_template_rendering()
        print("✅ 测试2通过")
        
        print("\n🎉 所有测试都通过了！")
        
    except Exception as e:
        print(f"\n❌ 测试失败: {e}")
        return False
    
    return True


def test_template_rendering():
    """测试Jinja模板渲染功能"""
    from jinja2 import Environment, FileSystemLoader
    
    # 设置模板环境
    template_dir = Path("static/prompts")
    if not template_dir.exists():
        print("⚠️ 模板目录不存在，跳过模板测试")
        return
    
    env = Environment(loader=FileSystemLoader(str(template_dir)))
    
    try:
        template = env.get_template("learning_assistant.jinja")
        
        # 测试数据
        exam_context = {
            "examId": "test_123",
            "startTime": "2024-01-15 10:00:00",
            "score": 75,
            "totalScore": 100,
            "totalQuestions": 3,
            "correctCount": 1,
            "totalTime": 1800,
            "questions": [
                {
                    "id": "q1",
                    "title": "测试题目1",
                    "questionType": "单选题",
                    "answer": "A",
                    "userAnswer": {
                        "answerContent": "B",
                        "isCorrect": 0
                    }
                }
            ]
        }
        
        # 渲染模板
        rendered = template.render(exam_context=exam_context)
        
        # 验证渲染结果
        required_items = ["考试详情", "test_123", "75", "单选题"]
        missing_items = [item for item in required_items if item not in rendered]
        
        if missing_items:
            raise AssertionError(f"模板缺少必要内容: {missing_items}")
        
        print("📝 模板渲染成功，包含所有必要信息")
        
    except Exception as e:
        raise Exception(f"模板渲染失败: {e}")


if __name__ == "__main__":
    success = asyncio.run(run_all_tests())
    sys.exit(0 if success else 1)
